package com.jiuyun.servlet;

import com.google.gson.Gson;
import com.jiuyun.dao.BookDao;
import com.jiuyun.dao.SettingsDao;
import com.jiuyun.dao.impi.BookDaoImpl;
import com.jiuyun.dao.impi.SettingsDaoimpl;
import com.jiuyun.entity.Book;
import com.jiuyun.entity.Question;
import com.jiuyun.entity.Settings;
import com.jiuyun.util.GetCode;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/set/*")
public class SettingsServlet extends BaseServlet{
        SettingsDao dao=new SettingsDaoimpl();
    protected void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        request.setCharacterEncoding("UTF-8");
        int sum=0;
        String kscode= GetCode.Getcode();
        do{ sum=dao.addcode(kscode);}while(sum==0);
        request.getSession().setAttribute("CODE",kscode);

        //用户id
        int uid=(Integer) request.getSession().getAttribute("uid");
        do{
        String ksid= GetCode.Getcode();
        Settings s=new Settings(ksid,request.getParameter("ksname"),
                0, request.getParameter("kstxt"),kscode,Integer.parseInt(request.getParameter("kszf")),
                Integer.parseInt(request.getParameter("ksdate")),request.getParameter("kskdate"),
                request.getParameter("ksjdate"), uid);
        sum=dao.add(s);
        }while (sum==0);
        System.out.println("成功！");
    }

    protected void up(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        int i=Integer.parseInt(request.getParameter("i"));
        String bmid=(String) request.getSession().getAttribute("CODE");
        try {
            dao.up(i,bmid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    protected void getsj(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Settings> list=new ArrayList<Settings>();
        //用户
        int uid=(Integer) request.getSession().getAttribute("uid");
        try {
            list=dao.getsj(uid);
            System.out.println(list);
            request.getSession().setAttribute("getsj",list);
            response.sendRedirect("http://localhost:9090/Eventstar/settings_zw/exam.jsp");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    protected void tz(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String bmid=request.getParameter("ebm");
        request.getSession().setAttribute("CODE",bmid);
        String name=request.getParameter("ename");
        int zf=Integer.parseInt(request.getParameter("ezf"));
        List<Question> list = null;
        try {
            list=dao.gettimu(bmid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        request.getSession().setAttribute("GETTM",list);
        response.sendRedirect("http://localhost:9090/Eventstar/settings_zw/settings.jsp?URL=settings_zw/edit.jsp&name="+name+"&fra="+zf);
    }
}
